package com.xxhg;

import org.apache.commons.math3.linear.Array2DRowRealMatrix;
import org.apache.commons.math3.linear.RealMatrix;
import org.apache.commons.math3.stat.correlation.Covariance;
import org.apache.commons.math3.stat.correlation.PearsonsCorrelation;

/**
 * 线性相关矩阵测试
 */
public class MatrixConversionExample {
    public static void main(String[] args) {
        // 创建一个数据矩阵
        double[][] data = {
                {1.0, 100.0, 1, 100, 1, 52},
                {2.0, 150.0, 2, 100, 2, 54},
                {1.0, 150.0, 2, 80,  1, 51},
                {1.0, 200.0, 3, 90,  2, 56},
                {3.0, 100.0, 4, 90,  1, 55},
                {2.0, 150.0, 5, 80,  2, 52},
                {3.0, 200.0, 6, 100, 1, 51},
                {2.0, 100.0, 6, 100, 1, 58},
                {1.0, 150.0, 1, 90,  1, 57},
                {1.0, 170.0, 2, 85, 1, 50},
                {1.0, 210.0, 3, 95, 2, 57},
                {3.0, 110.0, 4, 95, 1, 54},
                {2.0, 160.0, 5, 85, 2, 53},
                {3.0, 210.0, 6, 110, 1, 52},
                {2.0, 120.0, 6, 110, 1, 59},
                {1.0, 170.0, 1, 95, 1, 56}
        };

        // 创建一个 Pearson 相关系数计算对象
        PearsonsCorrelation correlation = new PearsonsCorrelation();

        // 计算相关系数矩阵
        RealMatrix realCorrelationMatrix = correlation.computeCorrelationMatrix(data);

        // 将 RealMatrix 转换为 double[][]
        double[][] correlationMatrix = realCorrelationMatrix.getData();

        // 打印转换后的 double[][]
        for (double[] row : correlationMatrix) {
            for (double value : row) {
                System.out.printf("%.4f ", value);
            }
            System.out.println();
        }
    }
}
